package cn.group7.padserver.service.impl;

import cn.group7.padserver.dao.ReserveDao;
import cn.group7.padserver.pojo.Reserve;
import cn.group7.padserver.service.ReserveService;
import jakarta.annotation.Resource;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 谷汉斌
 * @description
 * @createTime 2024/8/4 下午2:35
 */
@Service
public class ReserveServiceImpl implements ReserveService {

    @Resource
    private ReserveDao reserveDao;


    /**
     * 添加预约
     *
     * @param reserve 预约对象
     * @return 结果
     */
    @Override
    public boolean insert(Reserve reserve) {
        UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        reserve.setQuestioner(name);
        return reserveDao.insertSelective(reserve) == 1;
    }

    @Override
    public Reserve selectById(Integer id) {
        return reserveDao.selectByPrimaryKey(id);
    }


    @Override
    public boolean updateById(Reserve reserve) {
        return reserveDao.updateByPrimaryKeySelective(reserve) == 1;
    }

    @Override
    public boolean deleteById(Integer id) {
        return reserveDao.deleteByPrimaryKey(id) == 1;
    }

    @Override
    public List<Reserve> selectByKind(String kind) {
        UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        Reserve reserve = new Reserve();
        if ("questioner".equals(kind)) {
            reserve.setQuestioner(name);
        } else {
            reserve.setExpertName(name);
        }
        return reserveDao.selectByReserve(reserve);
    }
}
